package b_lambda;

import a_arrays.Person;

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.IntToDoubleFunction;

//使用Lambda表达式对sort排序方法做优化
public class LambdaDemo02 {
    public static void main(String[] args) {
        //把数组中的原数据修改为新数据后又存入到数组中
        double[] prices = {99.9,128,100};


        //匿名内部类的实现方式:
        //把所有的价格打八折,然后又继续存入数组中
        Arrays.setAll(prices, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int value) {
                //value = 0 1 2
                return prices[value] * 0.8;
            }
        });
        System.out.println(Arrays.toString(prices));//[79.92000000000002, 102.4, 80.0]



        Person[] persons = new Person[4]; //[null,null,null,null]
        persons[0] = new Person("lucy",169.8,23);
        persons[1] = new Person("marry",169,18);
        persons[2] = new Person("tom",180,24);
        persons[3] = new Person("jack",189.7,33);

        Arrays.sort(persons, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return Double.compare(o1.getHeight(),o2.getHeight());//升序
            }
        });
        System.out.println(Arrays.toString(persons));


        System.out.println("--------------------------------------------");


        //lambda表达式的方式:
        Arrays.setAll(prices, (int value) -> {return prices[value] * 0.8;});
        System.out.println(Arrays.toString(prices));

        //sort() 的优化
        Arrays.sort(
                persons,
                (Person o1, Person o2) -> {
                    return Double.compare(o1.getHeight(),o2.getHeight());
                }
        );
        System.out.println(Arrays.toString(persons));




    }
}







